package hihocoder;
import java.util.Scanner;

/**
 * 
 */

/**
 * @author zhongfang
 *
 */
public class 数组重排2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int N=scanner.nextInt();
		int[] A=new int[N];
		for (int i = 0; i < N; i++) {
			A[i]=scanner.nextInt();
		}
		int count=0;
		int lastIndex=0;
		while (true) {
			int min=A[0];
			int i=0;
			while (i<N-1&&A[i+1]>A[i]) {
				if(min>A[i]){
					min=A[i];
				}
				i++;
			}
			if(i==N-1){
				break;
			}
			//找到min-1所在位置
			int target=min-1;
			while (A[i]!=target) {
				i++;
			}
			lastIndex=i;
//			for (int i = 0; i < A.length; i++) {
//				diff[i]=A[i]-i;
//				if(diff[i]<0){
//					stopTag=false;
//					if(max<A[i]){
//						lastIndex=i;
//						max=A[i];
//					}
//				}
//			}
				count++;
				//拿走最后一个负数的,放到最前面
				int tmp=A[lastIndex];
				for (int j = lastIndex-1; j >=0; j--) {
					A[j+1]=A[j];
				}
				A[0]=tmp;//放到左侧
		}
		System.out.println(count);
	}
}
